package com.ktgj.customer.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import com.ktgj.common.core.enums.EmHelper;
import com.ktgj.customer.domain.CaseClaimPhoto;
import com.ktgj.customer.domain.GuaranteeCase;
import com.ktgj.customer.domain.vo.DelayedCareVo;
import com.ktgj.customer.domain.vo.GuaranteeCaseParameterVo;
import com.ktgj.customer.domain.vo.TmpDelayedCareVo;
import com.ktgj.customer.mapper.CaseClaimPhotoMapper;
import com.ktgj.customer.mapper.GuaranteeCaseMapper;
import com.ktgj.customer.service.IGuaranteeCaseService;
import com.ktgj.customer.service.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktgj.customer.mapper.CaseClaimMapper;
import com.ktgj.customer.domain.CaseClaim;
import com.ktgj.customer.service.ICaseClaimService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 理赔工单Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-05-24
 */
@Transactional
@Service
public class CaseClaimServiceImpl implements ICaseClaimService 
{
    @Autowired
    private CaseClaimMapper caseClaimMapper;
    @Autowired
    private ILoginService loginService;
    @Autowired
    private CaseClaimPhotoMapper caseClaimPhotoMapper;
    @Resource
    private GuaranteeCaseMapper guaranteeCaseMapper;
    @Resource
    private IGuaranteeCaseService guaranteeCaseService;

    /**
     * 查询理赔工单
     * 
     * @param caseClaimId 理赔工单ID
     * @return 理赔工单
     */
    @Override
    public CaseClaim selectCaseClaimById(Long caseClaimId)
    {
        GuaranteeCase guaranteeCase = guaranteeCaseMapper.selectCaseClaimIdById(caseClaimId);
        CaseClaim caseClaim = caseClaimMapper.selectCaseClaimById(caseClaimId);
        List<CaseClaimPhoto> caseClaimPhotos = caseClaimPhotoMapper.selectCaseClaimPhotoListByCaseClaimId(caseClaimId);
        if (caseClaimPhotos != null && caseClaimPhotos.size() > 0)
        {
            caseClaim.getCaseClaimPhotos().addAll(caseClaimPhotos);
            caseClaim.setCaseId(guaranteeCase.getCaseId());
        }
        try
        {
            GuaranteeCaseParameterVo guaranteeCaseParameterVo = guaranteeCaseService.backGuaranteeCaseById(guaranteeCase.getCaseId());
            caseClaim.setGuaranteeCase(guaranteeCaseParameterVo);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return caseClaim;
    }

    /**
     * 查询理赔工单列表
     * 
     * @param caseClaim 理赔工单
     * @return 理赔工单
     */
    @Override
    public List<CaseClaim> selectCaseClaimList(CaseClaim caseClaim)
    {
        return caseClaimMapper.selectCaseClaimList(caseClaim);
    }

    /**
     * 新增理赔工单
     * 
     * @param caseClaim 理赔工单
     * @return 结果
     */
    @Override
    public int insertCaseClaim(CaseClaim caseClaim)
    {
        return caseClaimMapper.insertCaseClaim(caseClaim);
    }

    /**
     * 修改理赔工单
     * 
     * @param caseClaim 理赔工单
     * @return 结果
     */
    @Override
    public int updateCaseClaim(CaseClaim caseClaim)
    {
        caseClaimPhotoMapper.deleteCaseClaimPhotoByClaimId(caseClaim.getCaseClaimId());
        for (CaseClaimPhoto caseClaimPhoto: caseClaim.getCaseClaimPhotos())
        {
            caseClaimPhoto.setCaseClaimId(caseClaim.getCaseClaimId());
            caseClaimPhotoMapper.insertCaseClaimPhoto(caseClaimPhoto);
        }
        if (caseClaim.getGuaranteeCase() != null)
        {
            GuaranteeCaseParameterVo guaranteeCaseParameterVo = caseClaim.getGuaranteeCase();
            GuaranteeCase guaranteeCase = new GuaranteeCase();
            guaranteeCase.setCaseId(guaranteeCaseParameterVo.getCaseId());
            guaranteeCase.setCaseStatus(Long.valueOf(guaranteeCaseParameterVo.getWorkOrderStatus()));
            guaranteeCaseService.updateGuaranteeCase(guaranteeCase);
        }
        return caseClaimMapper.updateCaseClaim(caseClaim);
    }

    /**
     * 批量删除理赔工单
     * 
     * @param caseClaimIds 需要删除的理赔工单ID
     * @return 结果
     */
    @Override
    public int deleteCaseClaimByIds(Long[] caseClaimIds)
    {
        return caseClaimMapper.deleteCaseClaimByIds(caseClaimIds);
    }

    /**
     * 删除理赔工单信息
     * 
     * @param caseClaimId 理赔工单ID
     * @return 结果
     */
    @Override
    public int deleteCaseClaimById(Long caseClaimId)
    {
        return caseClaimMapper.deleteCaseClaimById(caseClaimId);
    }

    /**
     * 延误关怀
     * */
    @Override
    public List<DelayedCareVo> caseClaimList(HttpServletRequest request) throws Exception {
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM月dd日");
        SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Long customerId;
        try {
            customerId = loginService.getLoginCustomer(request).getCustomerId();
        } catch (Exception e){
            throw new Exception(e.getMessage());
        }

        List<DelayedCareVo> delayedCareVoList = new ArrayList<>();
        List<CaseClaim> caseClaims = caseClaimMapper.caseClaimList(customerId);
        if (caseClaims.size() > 0){
            for (CaseClaim caseClaim : caseClaims) {
                DelayedCareVo delayedCareVo = new DelayedCareVo();
                delayedCareVo.setCaseClaimId(caseClaim.getCaseClaimId());
                if (caseClaim.getLatedate() != null && !caseClaim.getLatedate().equals("")){
                    delayedCareVo.setTicktWay(caseClaim.getFlightno() + dateFormat.format(caseClaim.getLatedate()));
                }

                delayedCareVo.setDeparture(caseClaim.getDeparture());
                delayedCareVo.setArrival(caseClaim.getArrival());

                if (caseClaim.getRealFlyawayDate() != null && !caseClaim.getRealFlyawayDate().equals("")){
                    delayedCareVo.setDepartureTime(timeFormat.format(caseClaim.getRealFlyawayDate()));
                }

                if (caseClaim.getReallandingtime() != null && !caseClaim.getReallandingtime().equals("")){
                    delayedCareVo.setScheduledLandingTime(timeFormat.format(caseClaim.getReallandingtime()));
                }

                delayedCareVo.setFlightStatus(caseClaim.getFlightstatus());
                delayedCareVo.setInSno(caseClaim.getInsno());

                if (caseClaim.getIsvalid() != null && !caseClaim.getIsvalid().equals("")){
                    //符合理赔
                    if (caseClaim.getIsvalid().equals(EmHelper.workOrderType.EligibleClaims.getValue())){
                        delayedCareVo.setIsValId("符合理赔");
                    }
                    //拒绝理赔
                    else if (caseClaim.getIsvalid().equals(EmHelper.workOrderType.DenyClaim.getValue())){
                        delayedCareVo.setIsValId("拒绝理赔");
                    }
                    //未判断
                    else if (caseClaim.getIsvalid().equals(EmHelper.workOrderType.NotJudged.getValue())){
                        delayedCareVo.setIsValId("未判断");
                    }
                    //已理赔
                    else if (caseClaim.getIsvalid().equals(EmHelper.workOrderType.Claimed.getValue())){
                        delayedCareVo.setIsValId("已理赔");
                    }
                    //已核销
                    else if (caseClaim.getIsvalid().equals(EmHelper.workOrderType.WrittenOff.getValue())){
                        delayedCareVo.setIsValId("已核销");
                    }
                    //资料不符合理赔
                    else if (caseClaim.getIsvalid().equals(EmHelper.workOrderType.Incompatible.getValue())){
                        delayedCareVo.setIsValId("资料不符合理赔");
                    }
                    //已请款理赔中
                    else if (caseClaim.getIsvalid().equals(EmHelper.workOrderType.AlreadyApplied.getValue())){
                        delayedCareVo.setIsValId("已请款理赔中");
                    }
                    //待审核
                    else if (caseClaim.getIsvalid().equals(EmHelper.workOrderType.PendingReview.getValue())){
                        delayedCareVo.setIsValId("待审核");
                    }
                }
                delayedCareVo.setIsValId(caseClaim.getIsvalid());

                if (caseClaim.getCreateTime() != null && !caseClaim.getCreateTime().equals("")){
                    delayedCareVo.setCreateTime(format.format(caseClaim.getCreateTime()));
                }

                List<TmpDelayedCareVo>  tmpDelayedCareVoList = new ArrayList<>();
                List<CaseClaimPhoto> caseClaimPhotos = caseClaimPhotoMapper.selectCaseClaimPhotoListByCaseClaimId(caseClaim.getCaseClaimId());
                for (CaseClaimPhoto caseClaimPhoto : caseClaimPhotos) {
                    TmpDelayedCareVo tmpDelayedCareVo = new TmpDelayedCareVo();
                    tmpDelayedCareVo.setCaseClaimPhotoId(caseClaimPhoto.getCaseClaimPhotoId());
                    tmpDelayedCareVo.setClaimPhotoPath(caseClaimPhoto.getClaimPhotoPath());
                    if (caseClaimPhoto.getClaimPhotoType() != null && !caseClaimPhoto.getClaimPhotoType().equals("")){
                        //图片
                        if (caseClaimPhoto.getClaimPhotoType().equals(EmHelper.workOrderType.Picture.getValue())){
                            tmpDelayedCareVo.setClaimPhotoType("图片");
                        }
                        else if (caseClaimPhoto.getClaimPhotoType().equals("workOrder")){
                            tmpDelayedCareVo.setClaimPhotoType("工单图片");
                        }
                    }
                    tmpDelayedCareVoList.add(tmpDelayedCareVo);
                }
                delayedCareVo.setTmpDelayedCareVoList(tmpDelayedCareVoList);
                delayedCareVoList.add(delayedCareVo);
            }
        }
        return delayedCareVoList;
    }
}
